package com.wmyg.antifake.utils;

import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	/**
	 * 时间戳转换成日期格式字符串
	 * @param seconds 精确到秒的字符串
	 * @param
	 * @return
	 */
	public static String timeStamp2Date(String seconds,String format) {
		if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
			return "";
		}
		if(format == null || format.isEmpty()){
			format = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date(Long.valueOf(seconds+"000")));
	}
	/**
	 * 日期格式字符串转换成时间戳
	 * @param date_str 字符串日期
	 * @param format 如：yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String date2TimeStamp(String date_str,String format){
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return String.valueOf(sdf.parse(date_str).getTime()/1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 取得当前时间戳（精确到秒）
	 * @return
	 */
	public static String timeStamp(){
		long time = System.currentTimeMillis();
		String t = String.valueOf(time/1000);
		return t;
	}


	public static String getTimeDifferenceStr(Date start, Date end) {
		if (start == null) {
			throw new IllegalArgumentException("start time must not null");
		} else if (end == null) {
			throw new IllegalArgumentException("end time must not null");
		}
		long between = (end.getTime() - start.getTime()) / 1000;

		long day = between / (24 * 3600);

		long hour = between % (24 * 3600) / 3600;

		long minute = between % 3600 / 60;

		long second = between % 60 / 60;
		StringBuffer timeStr = new StringBuffer();
		timeStr.append(day).append("天").append(hour).append("小时").append(minute).append("分钟").append(second)
				.append("秒");

		return timeStr.toString();
	}

	//TODO 计算时分秒
	public static String secToTime(int time) {
		String timeStr = null;
		int hour = 0;
		int minute = 0;
		int second = 0;
		if (time <= 0)
			return "00:00";
		else {
			minute = time / 60;
			hour = minute / 60;
			if (hour > 99)
				return "99:59:59";
			minute = minute % 60;
			second = time - hour * 3600 - minute * 60;
			timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);

		}
		return timeStr;
	}

	private static String unitFormat(int i) {
		String retStr = null;
		if (i >= 0 && i < 10)
			retStr = "0" + Integer.toString(i);
		else
			retStr = "" + i;
		return retStr;
	}

	//TODO 转换计算时分秒
	public static String timeStr(int time){
		String stamp =secToTime(time);
		StringBuffer stamp_str = new StringBuffer();
		String[] str = stamp.split(":");
		for(int i=0;i<str.length;i++){
			if(str[i].length() == 2 && str[i].substring(0, 1) .equals("0") ){
				str[i] = str[i].substring(1, 2);
			}
			if(i == 0){
				stamp_str.append(str[i] +"时");
			}else if(i == 1){
				stamp_str.append(str[i] +"分");
			}else if(i==2){
				stamp_str.append(str[i] +"秒");
			}
		}
		return stamp_str.toString();
	}
	public static Date fomatDate(String date) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return fmt.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	public static Date fomatToDate(String date,String formatStr) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(StringUtils.isNotEmpty(formatStr)){
			fmt = new SimpleDateFormat(formatStr);
		}
		try {
			return fmt.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	public static String fomatDateStr(Date date,String formatStr) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(null!=formatStr&&formatStr!=""){
			 format = new SimpleDateFormat(formatStr);
		}
		String str="";
		try {
			str = format.format(date);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		return str;
	}
	public static Date getSumDaysTime(Date time,int days){
		Calendar c = Calendar.getInstance();
		c.setTime(time);
		c.add(Calendar.DAY_OF_MONTH, days);
		return c.getTime();
	}
}
